<?php
require_once('config.php');

$user_id=intval($_POST['user_id']);
$last_msg = intval($_POST['last_msg']);

$query="SELECT message_id, message, time(sent) as time, p_name, sender, receiver, type 
FROM Message 
INNER JOIN Player 
ON sender=player_id 
WHERE 
message_id>$last_msg 
AND 
((receiver=$user_id OR receiver=0) OR sender=$user_id)
ORDER BY message_id, receiver, time";

$res=mysql_query($query);
$messages=array();

//echo $query;

while($message=mysql_fetch_assoc($res)){
	array_push($messages, $message);
}
echo json_encode($messages);

?>
